package com.example.onlineeducation.adapter

import android.annotation.SuppressLint
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.view.View
import com.example.onlineeducation.R
import com.example.onlineeducation.data.constant.ProblemType
import com.example.onlineeducation.data.constant.QuestionType
import com.example.onlineeducation.data.entity.ExamAnswerEntity
import com.example.onlineeducation.data.entity.ExamOptionEntity
import com.example.onlineeducation.databinding.ExamResultQuestionItemBinding
import com.example.onlineeducation.databinding.ExamResultQuestionOptionItemBinding
import com.example.onlineeducation.utils.YcSpannable
import com.yc.jetpacklib.extension.ycIsNotEmpty
import com.yc.jetpacklib.extension.ycSetTextColorRes
import com.yc.jetpacklib.init.YcJetpack
import com.yc.jetpacklib.recycleView.YcRecyclerViewAdapter
import com.yc.jetpacklib.utils.YcResources
import java.util.*

@SuppressLint("SetTextI18n")
class QuestionDetailAdapter : YcRecyclerViewAdapter<ExamAnswerEntity, ExamResultQuestionItemBinding>(ExamResultQuestionItemBinding::inflate) {

    private var mItemAdapter: OptionsAdapter? = null

    override var mOnUpdate2: (ExamResultQuestionItemBinding.(position: Int, data: ExamAnswerEntity) -> Unit)? = { position: Int, data: ExamAnswerEntity ->
        val questionTypeStr = "(${QuestionType.showQuestionTypeText(data.type)})"
        val contentStr = "${position + 1}、${data.content}"
        if (data.isRight == QuestionType.IS_RIGHT) {
            YcSpannable.setText(
                tvOptionsTitle,
                contentStr,
                questionTypeStr,
                YcJetpack.mInstance.getResources().getColor(R.color.problem_content_type)
            )
        } else {
            YcSpannable.setTextError(
                tvOptionsTitle,
                contentStr,
                questionTypeStr,
                YcJetpack.mInstance.getResources().getColor(R.color.selector_box_select_wrong),
                YcJetpack.mInstance.getResources().getColor(R.color.problem_content_type)
            )
        }

        if (data.options.ycIsNotEmpty()) {
            rvOptions.visibility = View.VISIBLE
            mItemAdapter = OptionsAdapter(data.userSelectOptionIds)
            rvOptions.adapter = mItemAdapter
            mItemAdapter?.addAllData(data.options)
        } else {
            rvOptions.visibility = View.GONE
        }
    }

}
